package app.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import app.model.Tbluser;
import app.service.IUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

@SuppressWarnings("serial")
public class UserAction extends ActionSupport {
	private static final Logger log = LoggerFactory.getLogger(UserAction.class);
	private Integer uid = new Integer(0); 
	private Tbluser u = new Tbluser(); 
	private List<Tbluser> userList = new ArrayList<Tbluser>();
	private IUserService usersService;
	private HttpServletRequest req;
	private HttpServletResponse resp;

	public Tbluser getU() {
		return u;
	}

	public void setU(Tbluser u) {
		this.u = u;
	}

	public IUserService getUsersService() {
		return usersService;
	}

	public void setUsersService(IUserService usersService) {
		this.usersService = usersService;
	}

	public String addUser() throws Exception {

		if (u == null) {
			log.debug("logout:","logout2");
			return ERROR;
		}
		if (usersService == null) {
			return ERROR;
		}
		usersService.addUsers(u);
		ActionContext ac = ActionContext.getContext();
		ac.put("new_user", u);
		ServletActionContext.getRequest().setAttribute("sac_user", u);
		/*
		 * UUID uu = UUID.randomUUID(); System.out.println(uu);
		 */

		return SUCCESS;
	}
	
	public String find()throws Exception
	{
		Tbluser user = this.usersService.find("");
		if(null!=user)
		{
			if(user.getName().equals("admin"))
			{
			/*	req = ServletActionContext.getRequest();
				req.setAttribute("user", user);*/
				
				ActionContext ac = ActionContext.getContext();
				ac.getSession().put("user", user); 
				
				return SUCCESS;				
			}
		}
		return ERROR;
	}

	public String findALL() throws Exception {
		List<Tbluser> users = usersService.findAll();
		
		req = ServletActionContext.getRequest();
		req.setAttribute("req_users", users);
// 		ActionContext ac = ActionContext.getContext();
//		ac.getSession().put("users", users);
		
		return SUCCESS;
	}

	public String delUser() throws Exception {
		//this.usersService.notify();
		req = ServletActionContext.getRequest();
		String str_id = (String) req.getAttribute("id");//获取request属性
		
		str_id = ServletActionContext.getRequest().getParameter("id");//获取request 参数
		System.out.println(str_id);
		
		int id = Integer.parseInt(str_id);
		this.usersService.delUser(id);
		return SUCCESS;
	}
	
	public String editUser() throws IOException{
		//this.usersService.updateUser(user);
		try{int a = 3/0;}catch(Exception e)
		{
			resp = ServletActionContext.getResponse();
			resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "error!");			
			//return null;
		}

		return SUCCESS;
	}
}
